import type { MenuItemData } from "@/types/index";

export const getTreeFromList = (list: any, count: number = 2) => {
    const root = list.filter((item: { parent_id: number }) => item.parent_id == 0)
    const type = 1

    const newlist = root.map((item: any) => {
        const children = getTreeChildren(list, item.menu_id, type, count)
        return {
            ...item,
            children
        }
    })
    return newlist
}

export const getTreeChildren = (list: any, id: number, type: number, count: number) => {
    type += 1

    const nodes = list.filter((item: { parent_id: number; }) => item.parent_id == id)
    const child = nodes.map((item1: { menu_id: number; }) => {
        let children: any = []
        if (type < count) {
            children = getTreeChildren(list, item1.menu_id, type, count)
        }
        return {
            ...item1,
            children
        }
    })
    return child
}